package artid.com.arttogo.activity;

import java.util.List;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.R.integer;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.StrictMode;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.webkit.WebView.HitTestResult;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.RelativeLayout.LayoutParams;
import android.widget.Button;
import android.widget.HorizontalScrollView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import com.android.volley.RequestQueue;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.ImageLoader.ImageListener;
import com.android.volley.toolbox.Volley;
import com.bumptech.glide.Glide;
import artid.com.arttogo.R;
import artid.com.arttogo.bean.CancelOrder;
import artid.com.arttogo.bean.InfoOrder;
import artid.com.arttogo.db.Cache;
import artid.com.arttogo.network.VolleyBasic;
import artid.com.arttogo.network.VolleyCallback;
import artid.com.arttogo.network.VolleyData;
import artid.com.arttogo.network.VolleyMap;
import artid.com.arttogo.utils.ToastUtil;

/**
 * TODO<请描述这个类是干什么的> 订单详情
 * 
 * @author 熊彬
 * @data: 2016-1-5 上午11:24:06
 * @version: V1.0
 */
public class OrderDetActivity extends BaseActivity implements
		OnClickListener {

    TextView stateText, numText, timeText, artnameText, artCateText,
            artSizeText, artPriceText, nameText, phoneText, addressText,
            payText, expressText;

    View expressView,payView,bottomView,expressAllView;

    Button cancel, ensure,payBtn;

    ImageView imgMain;
    String orderid = "";
    String orderNum;//订单号
    // String addressId = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_order_det);
        orderid = getIntent().getExtras().getString("id");
        mContext = OrderDetActivity.this;
        expressAllView = (View)findViewById(R.id.order_det_view_express);
        payView = (View)findViewById(R.id.order_det_view_pay);
        bottomView = (View)findViewById(R.id.tag_order_det_bottom);
        mBack = (ImageView) findViewById(R.id.back_btn);
        mBack.setOnClickListener(this);
        mTittle = (TextView) findViewById(R.id.tittle_text);
        mTittle.setText("订单详情");
        stateText = (TextView) findViewById(R.id.det_order_state);
        numText = (TextView) findViewById(R.id.det_order_num);
        timeText = (TextView) findViewById(R.id.det_order_time);
        artnameText = (TextView) findViewById(R.id.det_order_name);
        artCateText = (TextView) findViewById(R.id.det_order_cate);
        artSizeText = (TextView) findViewById(R.id.det_order_size);
        artPriceText = (TextView) findViewById(R.id.det_order_price);
        nameText = (TextView) findViewById(R.id.det_order_address_name);
        phoneText = (TextView) findViewById(R.id.det_order_phone);
        addressText = (TextView) findViewById(R.id.det_order_address);
        payText = (TextView) findViewById(R.id.det_order_pay);
        expressText = (TextView) findViewById(R.id.det_order_express);
        expressView = (View) findViewById(R.id.det_order_view_express);
        expressView.setOnClickListener(this);
        cancel = (Button) findViewById(R.id.det_order_cancel);
        cancel.setOnClickListener(this);
        ensure = (Button) findViewById(R.id.det_order_ensure);
        payBtn = (Button) findViewById(R.id.det_order_btn_pay);
        imgMain = (ImageView) findViewById(R.id.det_order_img);
        reqData(orderid);
    }

    void reqData(String orderid) {
        VolleyBasic.getInstance(OrderDetActivity.this).post(Cache.infoOrder,
                VolleyMap.getInstance().infoOrder(orderid, "1"),
                InfoOrder.class, new VolleyCallback() {

                    @Override
                    public void onSuccess(VolleyData vd) {
                        // TODO Auto-generated method stub
                        final InfoOrder item = (InfoOrder) vd.getData();
                        String orderState = item.getOrder_state();
                        OrderDetActivity.this.orderid = item.getOrder_id();
                        if (orderState.equals("10")) {
                            stateText.setText("未付款");
                            payView.setVisibility(View.GONE);
                            expressAllView.setVisibility(View.GONE);
                            bottomView.setVisibility(View.VISIBLE);
                            payBtn.setOnClickListener(OrderDetActivity.this);
                        }else if(orderState.equals("20")){
                            stateText.setText("已付款");
                            bottomView.setVisibility(View.GONE);
                        }else if (orderState.equals("30")) {
                            stateText.setText("已发货");
                            bottomView.setVisibility(View.VISIBLE);
                            ensure.setVisibility(View.VISIBLE);
                            ensure.setOnClickListener(OrderDetActivity.this);
                            cancel.setVisibility(View.GONE);
                            payBtn.setVisibility(View.GONE);
                        }else if (orderState.equals("40")) {
                            stateText.setText("已收货");
                            bottomView.setVisibility(View.GONE);

                        }else if (orderState.equals("0")) {
                            stateText.setText("已取消");
                            payView.setVisibility(View.GONE);
                            expressAllView.setVisibility(View.GONE);
                            bottomView.setVisibility(View.GONE);
                        }
                        orderNum=item.getOrder_sn();
                        numText.setText(item.getOrder_sn());
                        timeText.setText(item.getAdd_time());
                        artnameText.setText(item.getGoods_name());
                        artCateText.setText(item.getGc_name());
                        artSizeText.setText(item.getGoods_size());
                        artPriceText.setText(item.getGoods_price());
                        nameText.setText(item.getTrue_name());
                        phoneText.setText(item.getMob_phone());
                        addressText.setText(item.getAddress());
                        if (item.getPayment_id().equals("2")) {
                            payText.setText("支付宝");
                        } else if (item.getPayment_id().equals("6")) {
                            payText.setText("微信支付");
                        } else {
                            payText.setText(item.getPayment_id());
                        }
                        expressText.setText(item.getE_name());
                        String width = String.valueOf(imgMain.getWidth());
                        Glide.with(mContext)
                                .load(item.getGoods_image() + "_" + width
                                        + "x10.png")
                                .placeholder(R.drawable.ic_menu_rotate)
                                .centerCrop().error(R.drawable.ic_menu_rotate)
                                .into(imgMain);



                        return;

                    }

                    @Override
                    public void onError(VolleyError volleyError) {
                        // TODO Auto-generated method stub

                    }
                });
    }


    void reqCancel(String orderid) {
        VolleyBasic.getInstance(OrderDetActivity.this).post(Cache.cancelOrder,
                VolleyMap.getInstance().cancelOrder(orderid, "0"),
                CancelOrder.class, new VolleyCallback() {

                    @Override
                    public void onSuccess(VolleyData vd) {
                        // TODO Auto-generated method stub
                        final CancelOrder item = (CancelOrder) vd.getData();
                        if (item.getState().equals("1")) {
                            finish();
                        }


                        return;

                    }

                    @Override
                    public void onError(VolleyError volleyError) {
                        // TODO Auto-generated method stub

                    }
                });
    }
    void reqState(String orderId, final String state) {
        VolleyBasic.getInstance((Activity) mContext).post(Cache.cancelOrder,
                VolleyMap.getInstance().cancelOrder(orderId, state),
                CancelOrder.class, new VolleyCallback() {

                    @Override
                    public void onSuccess(VolleyData vd) {
                        // TODO Auto-generated method stub
                        CancelOrder itemCancelOrder = (CancelOrder) vd
                                .getData();

                        if (itemCancelOrder.getState().equals("1")) {
                            if  (state.equals("40")) {
                                reqData(orderid);
                            }
                        }
                        return;

                    }

                    @Override
                    public void onError(VolleyError volleyError) {
                        // TODO Auto-generated method stub

                    }
                });
    }
    @Override
    public void onClick(View arg0) {
        // TODO Auto-generated method stub
        switch (arg0.getId()) {
            case R.id.enorder_viewtop_add:
                Intent intent = new Intent(OrderDetActivity.this,
                        ChoiceAddressActivity.class);
                startActivity(intent);
                break;
            case R.id.back_btn:
                finish();
                break;
            case R.id.det_order_view_express:
                if (TextUtils.isEmpty(orderid) || TextUtils.isEmpty(orderNum)) {
                    ToastUtil.toast(OrderDetActivity.this, "数据请求中");
                    return;
                }
                Intent intent2 = new Intent(OrderDetActivity.this, ExpressActivity.class);
                intent2.putExtra("id", orderid);
                intent2.putExtra("orderNum", orderNum);
                startActivity(intent2);
                break;
            case R.id.det_order_btn_pay:
                Intent intent3 = new Intent(mContext,ChoicePayActivity.class);
                intent3.putExtra("id", orderid);
                mContext.startActivity(intent3);
                break;
            case R.id.det_order_ensure:
                reqState(orderid,"40");
                break;
            case R.id.det_order_cancel:
                reqCancel(orderid);
                break;
            default:
                break;
        }
    }

}
